﻿using Microsoft.AspNetCore.Mvc;
using yunbo.Model.BI;
using yunbo.Repositorys.BIRepositorys;

namespace yunbo.Controllers.BIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class KSController : Controller
	{
		KSRepository ksRepository = new KSRepository();


		/// <summary>
		/// 获取客商
		/// </summary>
		/// <param name="state"></param>
		/// <param name="str"></param>
		/// <returns></returns>
		[HttpGet("GetKS")]
		public IActionResult GetKS(int state, string? str)
		{
			List<KS> ks = ksRepository.GetKS(state, str);
			return Ok(ks);
		}


		/// <summary>
		/// 获取客商
		/// </summary>
		/// <returns></returns>
		[HttpGet("GetOneKS")]
		public IActionResult GetOneKS(int? type)
		{
			List<KS> ks = ksRepository.GetOneKS(type);
			return Ok(ks);
		}


		/// <summary>
		/// 改变状态
		/// </summary>
		/// <param name="id"></param>
		/// <param name="state"></param>
		/// <returns></returns>
		[HttpPut("StateChange")]
		public IActionResult StateChange(int id, int state)
		{
			bool res = ksRepository.StateChange(id, state);
			return Ok(res);
		}


		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="ks"></param>
		/// <returns></returns>
		[HttpPost("AddKS")]
		public IActionResult AddKS(KS ks)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(ks.ks_code)
				|| string.IsNullOrEmpty(ks.ks_name)
				|| ks.ks_type <= 0
				|| string.IsNullOrEmpty(ks.ks_jc)
				|| ks.ks_level <= 0
				|| ks.ks_sx <= 0
				|| ks.ks_hy <= 0
				|| string.IsNullOrEmpty(ks.ks_person)
				|| string.IsNullOrEmpty(ks.ks_phone)
				|| string.IsNullOrEmpty(ks.ks_cz)
				|| string.IsNullOrEmpty(ks.ks_email)
				|| string.IsNullOrEmpty(ks.ks_address)
				|| ks.ks_gj <= 0
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = ksRepository.AddKS(ks);
			return Ok(res);
		}


		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="ks"></param>
		/// <returns></returns>
		[HttpPut("UpdateKS")]
		public IActionResult UpdateKS(KS ks)
		{
			// 数据验证（可选）
			if (string.IsNullOrEmpty(ks.ks_code)
				|| string.IsNullOrEmpty(ks.ks_name)
				|| ks.ks_type <= 0
				|| string.IsNullOrEmpty(ks.ks_jc)
				|| ks.ks_level <= 0
				|| ks.ks_sx <= 0
				|| ks.ks_hy <= 0
				|| string.IsNullOrEmpty(ks.ks_person)
				|| string.IsNullOrEmpty(ks.ks_phone)
				|| string.IsNullOrEmpty(ks.ks_cz)
				|| string.IsNullOrEmpty(ks.ks_email)
				|| string.IsNullOrEmpty(ks.ks_address)
				|| ks.ks_gj <= 0
				|| ks.ks_id <= 0
				)
			{
				return BadRequest("Invalid dictionary data."); // 如果数据无效，返回 400 Bad Request
			}

			bool res = ksRepository.UpdateKS(ks);
			return Ok(res);
		}


		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelKS")]
		public IActionResult DelKS(int id)
		{
			bool res = ksRepository.DelKS(id);
			return Ok(res);
		}


		/// <summary>
		/// 批量删除
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelKSs")]
		public IActionResult DelKSs(int[] ids)
		{
			bool res = ksRepository.DelKSs(ids);
			return Ok(res);
		}
        [HttpGet("GetName")]
        public IActionResult GetName(string ks_code)
        {
            string dict = ksRepository.GetName(ks_code);
            return Ok(dict);
        }
        [HttpGet("GetGYS")]
        public IActionResult GetGYS()
        {
            List<KS> ks = ksRepository.GetGYS();
            return Ok(ks);
        }

        [HttpGet("GetKSbycode")]
        public IActionResult GetKSbycode(string code)
        {
            KS kS = ksRepository.GetKSbycode(code);
            return Ok(kS);
        }
    }
}
